﻿using cnBlogs.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cnBlogs.DataModel;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
using AllanXingCommonLibrary;
using cnBlogLibrary;

namespace cnBlogs.ViewModel
{
    public class ArticleCategoryViewModel : GetFeedsBase
    {
        private ObservableCollection<ArticleCategoryInfo> _articleCategoryInfo;

        public ObservableCollection<ArticleCategoryInfo> ArticleCategoryInfoList
        {
            get { return _articleCategoryInfo; }
            set
            {
                this.SetProperty(ref _articleCategoryInfo, value);
            }
        }

        //this code wil optimize
        protected async override void Parse(string feed)
        {
            ArticleCategoryInfoList = new ObservableCollection<ArticleCategoryInfo>();

            MatchCollection matches = Regex.Matches(feed, "<li (class=\"cate_item_hover\" )?id=\"cate_item_(?<ID>.*?)\" onmouseover=\"cateShow\\u0028(?<ID>.*?)\\u0029\" onmouseout=\"cateHidden\\u0028(?<ID>.*?)\\u0029\">.*?</a>\r</li>");
            GetSubCategoryData subCategoryData = new GetSubCategoryData();
            await subCategoryData.PostString();

            foreach (Match item in matches)
            {
                ArticleCategoryInfo categoryinfo = new ArticleCategoryInfo();
                string id = item.Groups["ID"].Value;
                categoryinfo.CategoryID = id;
                GroupCollection gp = Regex.Match(item.Value, "<a href=\"(?<link>.*?)\">(?<Name>.*?)\\u0028(?<newcount>.*?)\\u0029</a>").Groups;
                categoryinfo.CategoryName = gp["Name"].Value;
                categoryinfo.DailyNewArticleCount = Convert.ToUInt32(gp["newcount"].Value);
                categoryinfo.CategoryLink = gp["link"].Value;
                if (subCategoryData.DictionartySubCategory.ContainsKey(id))
                {
                    categoryinfo.SubCategoryList = subCategoryData.DictionartySubCategory[id];
                }
                ArticleCategoryInfoList.Add(categoryinfo);
            }
        }

        protected override string GetUrl()
        {
            return AppResource.cnBlogsUlr;
        }
    }
}
